package com.dexilaonian.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.ViewTreeObserver;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.dexilaonian.R;
import com.dexilaonian.event.GlideCircleTransform;
import com.squareup.picasso.Picasso;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
public class ImageLoader {
    private static int measuredHeight;
    private static int measuredWidth;

    /**
     * 图像显示
     *
     * @param context
     * @param picUrl
     * @param image_iv
     */
    public static void display(Context context, String picUrl, ImageView image_iv) {
        if (TextUtils.isEmpty(picUrl)) {
            return;
        }

        Picasso.with(context).load(picUrl)
                /*.placeholder(R.drawable.weishangchuan)
                .error(R.drawable.weishangchuang)*/.into(image_iv);
    }
    public static void display1(Context context, String picUrl, ImageView image_iv) {
        if (TextUtils.isEmpty(picUrl)) {
            image_iv.setImageResource(R.drawable.user);
            return;
        }

        Picasso.with(context).load(picUrl)
                .placeholder(R.drawable.user)
                .error(R.drawable.user).into(image_iv);
    }
    public static void display2(Context context, String picUrl, ImageView image_iv) {
        if (TextUtils.isEmpty(picUrl)) {
            image_iv.setImageResource(R.drawable.kangyangxiangmutupian);
            return;
        }

        Picasso.with(context).load(picUrl)
                .placeholder(R.drawable.kangyangxiangmutupian)
                .error(R.drawable.kangyangxiangmutupian).into(image_iv);
    }
    /**
     * 图像显示  屏幕的最高的宽度 做了oss 的处理
     */
    public static void displayWindow(Context context, String picUrl, final ImageView image_iv) {
        if (TextUtils.isEmpty(picUrl)) {
            image_iv.setImageResource(R.drawable.tag_headportrait_bg);
            return;
        }
        if (PublicUtil.currentActivity !=null) {
            DisplayMetrics metric = new DisplayMetrics();
            PublicUtil.currentActivity.getWindowManager().getDefaultDisplay().getMetrics(metric);
            image_iv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    measuredHeight = image_iv.getMeasuredHeight();
                    image_iv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            });
            String pic = ImageZoomUtils.getPic(picUrl, (int) measuredHeight / 2, (int) metric.widthPixels / 2);
            display(context, pic, image_iv);
        } else {
            display(context, picUrl, image_iv);
        }
    }

    /**
     * 圆形图片
     */
    public static void displayCircle(Context context, String picUrl, ImageView image_iv) {
        if (TextUtils.isEmpty(picUrl)) {
            Glide.with(context)
                    .load(R.drawable.user)
                    .placeholder(R.drawable.user)
                    .error(R.drawable.user)
                    .transform(new GlideCircleTransform(context)).into(image_iv);
            return;
        }
        Glide.with(context)
                .load(picUrl)
                .placeholder(R.drawable.user)
                .error(R.drawable.user)
                .transform(new GlideCircleTransform(context)).into(image_iv);
    }
    /**
     * 圆形图片
     */
    public static void displayCircle1(Context context, String picUrl, ImageView image_iv) {
        if (TextUtils.isEmpty(picUrl)) {
            Glide.with(context)
                    .load(R.drawable.user)
                    .placeholder(R.drawable.user)
                    .error(R.drawable.user)
                    .skipMemoryCache(true)
                    .transform(new GlideCircleTransform(context)).into(image_iv);
            return;

        }
        Glide.with(context)
                .load(picUrl)
                .placeholder(R.drawable.user)
                .error(R.drawable.user)
                .skipMemoryCache(true)
                .transform(new GlideCircleTransform(context)).into(image_iv);

    }
    /**
     * 图像显示  正方形控件 做了oss 的处理
     *
     * @param context
     * @param picUrl
     * @param image_iv
     */
    public static void displaySquare(Context context, String picUrl, final ImageView image_iv) {
        if (TextUtils.isEmpty(picUrl)) {
            image_iv.setImageResource(R.drawable.tag_headportrait_bg);
            return;
        }
        if (PublicUtil.currentActivity != null) {
            image_iv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    measuredHeight = image_iv.getMeasuredHeight();
                    measuredWidth = image_iv.getMeasuredWidth();
                    image_iv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            });

            String pic = ImageZoomUtils.getPic(picUrl, (int) measuredHeight / 2, (int) measuredWidth / 2);
            display(context, pic, image_iv);
        } else {
            display(context, picUrl, image_iv);
        }
    }


    /**
     * @param context
     * @param picUrl
     * @param image_iv
     * @param wid
     * @param hei
     */
    public static void display(Context context, String picUrl, ImageView image_iv, int wid, int hei) {
        if (TextUtils.isEmpty(picUrl)) {
            image_iv.setImageResource(R.drawable.tag_headportrait_bg);
            return;
        }
        Picasso.with(context)
                .load(picUrl)
                .placeholder(R.drawable.pic_loading).error(R.drawable.tag_headportrait_bg)
                .resize(wid, hei)
                .into(image_iv);
    }

    /**
     * 展示大图，不加入缓存不加入内存
     *
     * @param context
     * @param imageUrl
     * @param image_iv
     */
/*    public static void diaplayBigImage(Context context, String imageUrl, ImageView image_iv) {
        if (TextUtils.isEmpty(imageUrl)) {
            image_iv.setImageResource(R.drawable.tag_headportrait_bg);
            return;
        }
        Picasso.with(context)
                .load(imageUrl)
                .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
                .into(image_iv);
    }*/

    /**
     * 展示本地图片文件
     *
     * @param context
     * @param picPath
     * @param image_iv
     */
    public static void displayFilePath(Context context, String picPath, ImageView image_iv) {
        if (TextUtils.isEmpty(picPath)) {
            image_iv.setImageResource(R.drawable.tag_headportrait_bg);
            return;
        }
        Picasso.with(context).load(new File(picPath))
                .placeholder(R.drawable.tag_headportrait_bg)
                .error(R.drawable.tag_headportrait_bg).into(image_iv);
    }

    public static boolean compressImage(File file) {
        String path = file.getPath();
        Bitmap bitmap = BitmapFactory.decodeFile(path);
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
// 开始读入图片，此时把options.inJustDecodeBounds设为true
        newOpts.inJustDecodeBounds = true;
        bitmap = BitmapFactory.decodeFile(path, newOpts);
        newOpts.inJustDecodeBounds = false;
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
// 设置分辨率
        float hh = 800f;
        float ww = 480f;
// 缩放比。由于是固定的比例缩放，只用高或者宽其中一个数据进行计算即可
        int be = 1;
        if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放
            be = (int) (newOpts.outWidth / ww);
        } else if (w < h && h > hh) {// 如果宽度大的话根据宽度固定大小缩放
            be = (int) (newOpts.outHeight / hh);
        }
        if (be <= 0) {
            be = 1;
        }
        newOpts.inSampleSize = be;// 设置缩放比例
// 重新读入图片，注意此时已经把newOpts.inJustDecodeBounds = false
        bitmap = BitmapFactory.decodeFile(path, newOpts);
        try {
            return compressImage2(bitmap);
        } catch (Exception e) {
// TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }

    public static boolean compressImage2(Bitmap bitmap) throws Exception {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            int options = 100;
            int size = baos.toByteArray().length / 1024;
            while (size > 40 && options > 0) {
                baos.reset();// 重置baos即清空baos
                options -= 10;// 每次都减少10
// 这里压缩options%，把压缩后的数据存放到baos中
                bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
                size = baos.toByteArray().length / 1024;
            }
// 把压缩后的数据baos存放到ByteArrayInputStream中
            ByteArrayInputStream isBm = new ByteArrayInputStream(
                    baos.toByteArray());
// 把ByteArrayInputStream数据生成图片
            bitmap = BitmapFactory.decodeStream(isBm, null, null);
            if (size > 40) {
                return true;
            } else {
                return false;
            }

        } catch (Exception e) {
            throw e;
        }

    }

}
